package config

import (
	"errors"
	"fmt"
	"gopkg.in/yaml.v3"
	"os"
)

var config Config

type Config struct {
	OrgId       string `yaml:"orgId"`
	MqttAddress string `yaml:"mqttAddress"`
	MqttPort    int    `yaml:"mqttPort"`
	//UserName      string `yaml:"userName"`
	//Password      string `yaml:"password"`
	ServerAddress string `yaml:"serverAddress"`
	LocalIp       string `yaml:"localIp"`
}

func GetConf() *Config {
	return &config
}

func init() {
	data, err := os.ReadFile("config.yaml")
	if err != nil {
		panic(err)
	}

	err = yaml.Unmarshal(data, &config)
	if err != nil {
		panic(err)
	}

	if config.OrgId == "" || config.MqttAddress == "" || config.MqttPort == 0 || config.ServerAddress == "" || config.LocalIp == "" {
		panic(errors.New("配置文件数据错误"))
	}

	fmt.Println("Load config file finish")
}
